package com.xxx.video.network.clients;



import com.xxx.video.commen.utils.L;
import com.xxx.video.network.configs.NetWorkConstants;
import com.xxx.video.network.requests.CommonRequest;
import com.xxx.video.network.requests.RequestParams;
import com.xxx.video.network.responses.DisposeDataHandle;
import com.xxx.video.network.responses.DownloadFileCallback;
import com.xxx.video.network.responses.Response2JsonCallback;

import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class CommonOkHttpClient {
    /**
     * this is a client
     */
    private static OkHttpClient mOkHttpClient;


    private static CommonOkHttpClient mCommonOkHttpClient = null;

    /*
     * static code
     */
    static {
        OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
        /*
         * set write time out
         */
        okHttpClientBuilder.writeTimeout(NetWorkConstants.NetWorkConfig.WRITE_TIME_OUT, TimeUnit.SECONDS);
        /*
         * set read time out
         */
        okHttpClientBuilder.readTimeout(NetWorkConstants.NetWorkConfig.READ_TIME_OUT, TimeUnit.SECONDS);
        /*
         * set connect time out
         */
        okHttpClientBuilder.connectTimeout(NetWorkConstants.NetWorkConfig.CONNECT_TIME_OUT, TimeUnit.SECONDS);
        /*
         *ssl cache...
         */
        /*
         * create client
         */
        mOkHttpClient = okHttpClientBuilder.build();
    }

    private CommonOkHttpClient() {

    }

    public static CommonOkHttpClient getInstance() {
        synchronized (CommonOkHttpClient.class) {
            if (mCommonOkHttpClient == null) {
                mCommonOkHttpClient = new CommonOkHttpClient();
            }
        }
        return mCommonOkHttpClient;
    }

    /**
     * send a Synchronize get request
     *
     * @param requestParams request params
     */
    public String getSynchronize(RequestParams requestParams) {
        Call getCall = null;
        Request request = null;
        try {
            request = CommonRequest.getInstance().createGetRequest(requestParams);
            getCall = mOkHttpClient.newCall(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         * look at  log
         */
        if (getCall != null) {
            try {
                Response response = getCall.execute();
                String responseString = response.body().string();
                L.i("request", "requestUrl And request Params" + request.toString() + "response\n" + responseString);
                return responseString;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }

    /**
     * send a  get request
     *
     * @param requestParams     request params
     * @param disposeDataHandle callback
     */
    public void get(RequestParams requestParams, DisposeDataHandle disposeDataHandle) {
        Call getCall = null;
        Request request = null;
        try {
            request = CommonRequest.getInstance().createGetRequest(requestParams);
            getCall = mOkHttpClient.newCall(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         */
        if (getCall != null) {
            getCall.enqueue(new Response2JsonCallback(disposeDataHandle, request));
        }
    }

    /**
     * send a  post request
     *
     * @param requestParams     request params
     * @param disposeDataHandle callback
     */
    public void postForm(RequestParams requestParams, DisposeDataHandle disposeDataHandle) {
        Call postFormCall = null;
        Request request = null;
        try {
            request = CommonRequest.getInstance().createPostFormRequest(requestParams);
            postFormCall = mOkHttpClient.newCall(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         */
        if (postFormCall != null) {
            postFormCall.enqueue(new Response2JsonCallback(disposeDataHandle, request));
        }
    }

    /**
     * this is a post json request
     *
     * @param requestParams     request params
     * @param disposeDataHandle callback
     */
    public void postJson(RequestParams requestParams, DisposeDataHandle disposeDataHandle) {
        Call posJsonCall = null;
        Request request = null;
        try {
            request = CommonRequest.getInstance().createPostJsonRequest(requestParams);
            posJsonCall = mOkHttpClient.newCall(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         */
        if (posJsonCall != null) {
            posJsonCall.enqueue(new Response2JsonCallback(disposeDataHandle, request));
        }
    }
    /**
     * send a Synchronize get request
     *
     * @param requestParams request params
     */
    public String postJsonSynchronize(RequestParams requestParams) {
        Call getCall = null;
        Request request = null;
        try {
            request = CommonRequest.getInstance().createPostJsonRequest(requestParams);
            getCall = mOkHttpClient.newCall(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         * look at  log
         */
        if (getCall != null) {
            try {
                Response response = getCall.execute();
                String responseString = response.body().string();
                L.i("request", "requestUrl And request Params" + request.toString() + "response\n" + responseString);
                return responseString;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return "";
    }
    /**
     * this is a download file request
     *
     * @param requestParams     request params
     * @param disposeDataHandle callback
     */
    public void downloadFile(RequestParams requestParams, DisposeDataHandle disposeDataHandle) {
        Call downloadFileCall = null;
        Request request = null;
        try {
            request = CommonRequest.getInstance().createGetRequest(requestParams);
            downloadFileCall = mOkHttpClient.newCall(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         */
        if (downloadFileCall != null) {
            downloadFileCall.enqueue(new DownloadFileCallback(disposeDataHandle));
        }
    }

    /**
     * this is a upload file request with progress
     *
     * @param requestParams     request params
     * @param disposeDataHandle callback
     */
    public void uploadFileProgress(RequestParams requestParams, DisposeDataHandle disposeDataHandle) {
        Call uploadFileCall = null;
        Request request = null;
        try {
            request = CommonRequest.getInstance().createMultipartRequest(requestParams, disposeDataHandle);
            uploadFileCall = mOkHttpClient.newCall(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
         *
         */
        if (uploadFileCall != null) {
            uploadFileCall.enqueue(new Response2JsonCallback(disposeDataHandle, request));
        }
    }
}
